using System;
using UnityEngine;
using Voodoo.Pattern;

public class Cam : MonoBehaviour
{
	public Transform _head;

	private float startXPos = 3.23f;

	private float lastXPos = 15.44f;

	private float startYPos = 5f;

	private float lastYPos = 15.44f;

	private LevelOrientationCameraFields rightcamera;

	private LevelOrientationCameraFields leftcamera;

	private LevelOrientationCameraFields topcamera;

	private LevelOrientationCameraFields archcamera;

	private LevelOrientationCameraFields currentCamera;

	private Camera camera;

	private bool inGame;

	private Vector3 BasePosition = new Vector3(Preloader.CameraConfig.x, Preloader.CameraConfig.y, Preloader.CameraConfig.z);

	public Transform custom;

	public float originalFOV;

	public float customFOV;

	public float lerpAmount;

	public Transform zoomedOut;

	public Camera cameraObject
	{
		get
		{
			if (camera == null)
			{
				camera = GetComponent<Camera>();
			}
			return camera;
		}
	}

	private void Awake()
	{
		rightcamera = new LevelOrientationCameraFields(1.64f, 3.23f, 15.44f, -20f, -20f, 6.23f, 15.44f);
		leftcamera = new LevelOrientationCameraFields(-1.64f, 20f, 20f, -1.23f, -9f, 6.23f, 15.44f);
		topcamera = new LevelOrientationCameraFields(0f, 0f, 13f, -0f, -13f, 6.23f, 15.44f);
		archcamera = new LevelOrientationCameraFields(0f, 1.5f, 13f, -1.5f, -13f, 6.23f, 15.44f);
		currentCamera = leftcamera;
	}

	private void Start()
	{
		SingletonMB<GameManager>.Instance.Cam = this;
		camera = GetComponent<Camera>();
		originalFOV = camera.fieldOfView;
		inGame = false;
		BasePosition = new Vector3(Preloader.CameraConfig.x, Preloader.CameraConfig.y, Preloader.CameraConfig.z);
		BasePosition = zoomedOut.position;
		if (Preloader.ShowLevelAtBeginning.IsEnabled || Preloader.Transition.IsEnabled)
		{
			ShowWholeLevel();
		}
		GameManager instance = SingletonMB<GameManager>.Instance;
		instance.OnGameStart = (Action)Delegate.Combine(instance.OnGameStart, new Action(StartGameCam));
		GameManager instance2 = SingletonMB<GameManager>.Instance;
		instance2.OnGameEnd = (Action)Delegate.Combine(instance2.OnGameEnd, new Action(MenuCam));
	}

	private void OnDisable()
	{
		if (!(SingletonMB<GameManager>.Instance == null))
		{
			GameManager instance = SingletonMB<GameManager>.Instance;
			instance.OnGameStart = (Action)Delegate.Remove(instance.OnGameStart, new Action(StartGameCam));
			GameManager instance2 = SingletonMB<GameManager>.Instance;
			instance2.OnGameEnd = (Action)Delegate.Remove(instance2.OnGameEnd, new Action(MenuCam));
		}
	}

	public void SetHead(Transform head)
	{
		_head = head;
	}

	public void SetCameraSide(LevelPosType t)
	{
		if (t == LevelPosType.Right)
		{
			currentCamera = rightcamera;
		}
		if (t == LevelPosType.Left)
		{
			currentCamera = leftcamera;
		}
		if (t == LevelPosType.Top)
		{
			currentCamera = topcamera;
		}
		if (t == LevelPosType.Arch)
		{
			currentCamera = archcamera;
		}
	}

	public void ShowWholeLevel()
	{
		custom = zoomedOut;
		customFOV = 45f;
		base.transform.position = new Vector3(custom.position.x, custom.position.y, BasePosition.z);
	}

	internal void ForceFollowCustom()
	{
		Vector3 position = new Vector3(custom.position.x, custom.position.y, BasePosition.z);
		base.transform.position = position;
	}

	public void StartGameCam()
	{
		inGame = true;
	}

	public void MenuCam()
	{
		inGame = false;
	}

	private void Update()
	{
		if (custom != null)
		{
			Vector3 b = new Vector3(custom.position.x, custom.position.y, BasePosition.z);
			base.transform.position = Vector3.Lerp(base.transform.position, b, Time.deltaTime);
			camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, customFOV, Time.deltaTime);
			lerpAmount = Time.deltaTime;
			return;
		}
		camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, originalFOV, Time.deltaTime);
		if ((bool)_head)
		{
			if (inGame)
			{
				base.transform.localPosition = Vector3.Lerp(base.transform.localPosition, new Vector3(_head.position.x, _head.position.y, zoomedOut.position.z), lerpAmount);
				lerpAmount = Mathf.Lerp(lerpAmount, 1f, Time.deltaTime);
			}
			else
			{
				base.transform.localPosition = BasePosition;
			}
		}
	}

	public void SetZeroPos()
	{
		base.transform.localPosition = new Vector3(0f, 2.5f, -10f);
	}
}
